import { ref, reactive } from 'vue'
import moment from 'moment'

function dateTimeFormat(time) {
  const format = '{y}-{m}-{d} {h}:{i}'
  let date = null

  if (typeof time === 'string') {
    time = parseInt(time)
  }
  date = new Date(time)

  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes()
  }
  const strTime = format.replace(/{(y|m|d|h|i)+}/g, (result, key) => {
    let value = formatObj[key]
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value
  })
  return strTime
}

// 时间发生变化存储的变量
const formatAllDatechange = ref('')

// 选择的时间
const formatAllDate = ref(moment().format('YYYY-MM-DD 08:00:00'))
const currentDate = ref([moment().format('MM月DD日今天'), '08时', '00分'])

// 默认时间
const initDate = (value) => {
  if (value) {
    formatAllDate.value = value
  }
}

// 含有年份的时间数组
let arrdate = reactive([])
// 格式化含有年份的时间数组
let formatArr = reactive([])
// 时  数组
const hourarr = reactive([])
// 分  数组
const minarr = reactive([])
// 存储三列数据的数组
const columns = ref([])

// 获取今年、去年、明年的开始、结束日期
// type为字符串类型，有两种选择，"s"代表开始,"e"代表结束，dates为数字类型，不传或0代表今年，-1代表去年，1代表明年
function getYear(type, dates) {
  var dd = new Date()
  var n = dates || 0
  var year = dd.getFullYear() + Number(n)
  if (type === 's') {
    return year + '-01-01'
  }
  if (type === 'e') {
    return year + '-12-31'
  }
  if (!type) {
    return year + '-01-01/' + year + '-12-31'
  }
  return ''
}

// 获取去年/今年/明年所有日期做成数组
function getchooseDay(begin, end) {
  var beginTime = begin.split('-')
  var endTime = end.split('-')
  var beginDay = new Date()
  beginDay.setUTCFullYear(beginTime[0], beginTime[1] - 1, beginTime[2])
  var endDay = new Date()
  endDay.setUTCFullYear(endTime[0], endTime[1] - 1, endTime[2])
  var unixDb = beginDay.getTime()
  var unixDe = endDay.getTime()
  const arrDate = []
  for (var k = unixDb; k <= unixDe; k++) {
    // arrDate.push((new Date(parseInt(k))).format())
    arrDate.push(moment(parseInt(k)).format('YYYY-MM-DD'))
    k = k + 24 * 60 * 60 * 1000
  }
  return arrDate
}

// 获取几月几号星期几
const dateFormat = (value) => {
  const d = new Date(value)
  const year = d.getFullYear()
  const current = new Date().getFullYear()
  const month = d.getMonth() > 8 ? d.getMonth() + 1 : '0' + (d.getMonth() + 1)
  const day = d.getDate() > 9 ? d.getDate() : '0' + d.getDate()
  const date = month + '月' + day + '日' + getdaytext(d.getDay())
  let time = ''
  if (year !== current) {
    time = year + '年' + date
  } else {
    time = date
  }
  return time
}
// 获取星期
const getdaytext = (weekNum) => {
  let week = ''
  switch (weekNum) {
    case 0:
      week = '周日'
      break
    case 1:
      week = '周一'
      break
    case 2:
      week = '周二'
      break
    case 3:
      week = '周三'
      break
    case 4:
      week = '周四'
      break
    case 5:
      week = '周五'
      break
    case 6:
      week = '周六'
      break
  }
  return week
}

// 获取列日期
const getcolumnsDate = () => {
  // 当前日期
  arrdate = getchooseDay(getYear('s', -1), getYear('e', 1)) // 去年/今年/明年所有日期
  formatArr = arrdate.map((it) => {
    return dateFormat(it) // 将日期的数组 item 加 周几
  })
  // 今天不显示周几 显示今天
  const time = dateTimeFormat(new Date().getTime())
  const index = currentIndexs(1, time)
  formatArr[index] = formatArr[index].toString().split('周')[0] + '今天'
  // 小时
  for (let i = 0; i < 24; i++) {
    hourarr.push(i > 9 ? i.toString() + '时' : '0' + i + '时')
  }

  // 分钟
  for (let i = 0; i < 60; i++) {
    minarr.push(i > 9 ? i.toString() + '分' : '0' + i + '分')
  }
  // 三列数据及默认选择
  const columnsDate = [
    {
      values: formatArr,
      className: 'dateTime',
      defaultIndex: currentIndexs(1, formatAllDate.value)
    },
    {
      values: hourarr,
      className: 'hourarr',
      defaultIndex: currentIndexs(2, formatAllDate.value)
    },
    {
      values: minarr,
      className: 'minarr',
      defaultIndex: currentIndexs(3, formatAllDate.value)
    }
  ]
  return columnsDate
}

// 获取默认时间的三列下标
const currentIndexs = (column, value) => {
  let index = 0
  const timeArr = value.replace(' ', ':').split(':')
  if (column === 1) {
    index = arrdate.map((item) => item).indexOf(timeArr[0])
  } else if (column === 2) {
    index = hourarr.map((item) => item).indexOf(timeArr[1] + '时')
  } else {
    index = minarr.map((item) => item).indexOf(timeArr[2] + '分')
  }

  return index
}

// 日期选择改变
const onChangeDate = ({ selectedValues }) => {
  let currentyear = ''
  let currentmonth = ''
  let currentday = ''
  const currenthour = selectedValues[1].replace('时', '') // 获取 月份 天 小时 也是为了判断下一级如何变化
  const cirremtminutes = selectedValues[2].replace('分', '')
  const type = isNaN(selectedValues[0].substr(0, 4))
  if (type) {
    // 当格式为xx月xx日
    currentyear = new Date().getFullYear()
    currentmonth = selectedValues[0].substr(0, 2)
    currentday = selectedValues[0].substr(3, 2)
  } else {
    // 当格式为xxxx年xx月xx日
    currentyear = selectedValues[0].substr(0, 4)
    currentmonth = selectedValues[0].substr(5, 2)
    currentday = selectedValues[0].substr(8, 2)
  }
  // 日期变化转换
  const currentminhou = currentyear + '-' + currentmonth + '-' + currentday + ' ' + currenthour + ':' + cirremtminutes
  formatAllDatechange.value = currentminhou
}

// 确认时间
const confirmchooseTime = () => {
  if (formatAllDatechange.value) {
    // 有选中值就得 赋值 currentminhou
    formatAllDate.value = formatAllDatechange.value
  }
}

export { columns, formatAllDate, currentDate, initDate, getcolumnsDate, onChangeDate, confirmchooseTime }
